
package com.idealighter.game.games.sirenniuniu.manager;

import com.alibaba.fastjson.TypeReference;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.games.common.niuniu.NiuNiuCard;
import com.idealighter.game.games.common.niuniu.NiuNiuCardsType;
import com.idealighter.game.games.common.niuniu.NiuNiuCardsTypeComparator;
import com.idealighter.game.games.common.niuniu.NiuNiuCardsTypeGetter;
import com.idealighter.game.games.sirenniuniu.struct.SiRenNiuNiuSeat;
import com.idealighter.game.games.sirenniuniu.struct.SiRenNiuNiuTable;
import com.idealighter.game.prizepool.manager.PlayerPrizePoolMgrScript;
import com.idealighter.game.prizepool.manager.RoomPrizePoolMgrScript;
import com.idealighter.game.prizepool.struct.control.SiRenNiuNiuControl;
import com.idealighter.game.prizepool.struct.player.PlayerPrizePool;
import com.idealighter.game.prizepool.struct.room.ControlStrategy;
import com.idealighter.game.prizepool.struct.room.RoomPrizePool;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 四人牛牛控制 .
 * 
 * @date 2016年1月20日 下午4:46:55
 *
 */
@Singleton
public class SiRenNiuNiuControlScript {

  private static final Logger LOG = LoggerFactory.getLogger(SiRenNiuNiuControlScript.class);

  // 控制手牌数量(控制时，提前随机生成的手牌数量)
  private static final int CONTROL_HAND_CARDS_NUM = 10;
  private static final Game THIS_GAME = Game.SIREN_NIUNIU;

  @Inject
  private SiRenNiuNiuMsgMgr msgMgr;
  @Inject
  private SiRenNiuNiuMgr srnnMgr;

  @Inject
  private PlayerPrizePoolMgrScript playerPrizePoolMgr;
  @Inject
  private RoomPrizePoolMgrScript roomPrizePoolMgr;

  /**
   * 控制玩家发牌,玩家和机器人玩才控制.
   * 
   * @param table .
   */
  public void controlDealCards(SiRenNiuNiuTable table) {
    // 庄家
    SiRenNiuNiuSeat dealer = table.dealer();
    boolean hasPlayer = false;
    boolean hasRobot = false;

    for (SiRenNiuNiuSeat s : table.getSeats()) {
      if (s.getPlayerId() > 0) {
        if (s.isRobot()) {
          hasRobot = true;
        } else {
          hasPlayer = true;
        }
      }
    }

    // 玩家和机器人玩才控制，个人奖池控制优先房间奖池控制
    if (hasPlayer && hasRobot) {

      /*
       * 控制发牌方法：先将7手玩家牌排序(从小到大),再根据庄家和奖池去计算玩家对应的手牌 .
       */
      // 需要从小到大排序(从小到大),一共7副手牌(庄家的牌在中间)
      LinkedList<List<NiuNiuCard>> sortedCardsList = new LinkedList<>();

      /*
       * 提前生成7副排好序的牌(why?除去庄家，最多3个闲家，闲家最多全部取头3张或者后3张) .
       */
      int cardIndex = 0;
      for (int i = 0; i < CONTROL_HAND_CARDS_NUM; i++) {
        sortedCardsList
            .add(table.getCards().subList(cardIndex, cardIndex += SiRenNiuNiuMgr.PLAYER_CARDS));
      }

      // 玩家的牌进行排序
      sortedCardsList.sort((cards1, cards2) -> {
        NiuNiuCardsType cards1Type = NiuNiuCardsTypeGetter.getCardsType(cards1, new ArrayList<>());
        NiuNiuCardsType cards2Type = NiuNiuCardsTypeGetter.getCardsType(cards2, new ArrayList<>());
        if (NiuNiuCardsTypeComparator.compare(cards1, cards1Type, cards2, cards2Type)) {
          return 1;
        } else {
          return -1;
        }
      });

      /*
       * 玩家计算奖池，庄家取sortedCardsList中间的手牌，比庄家大取提前生成的牌的头，反之取尾 . 先发庄家的牌,有了庄家的牌才能确定输赢计算筹码
       */
      List<NiuNiuCard> dealerCards = sortedCardsList.remove(CONTROL_HAND_CARDS_NUM / 2);
      dealDealerCards(dealer, dealerCards);

      for (SiRenNiuNiuSeat seat : table.getSeats()) {
        if (seat.getPlayerId() > 0 && seat.getPlayerId() != dealer.getPlayerId()) {
          // 庄家和闲家一个是机器人和玩家且有奖池控制
          if ((dealer.isRobot() && !seat.isRobot()) || (!dealer.isRobot() && seat.isRobot())) {
            // 玩家个人奖池控制发牌
            if (playerPrizePoolControlDealCards(dealer, seat, sortedCardsList)) {
              continue;
            }

            // 玩家房间奖池控制发牌
            if (roomPrizePoolControlDealCards(dealer, seat, sortedCardsList)) {
              continue;
            }
          }

          // 没有奖池控制随机输赢取排序好的牌发
          if (RandCodeUtil.randomBoolean()) {
            dealPlayerCards(seat, sortedCardsList.removeLast());
          } else {
            dealPlayerCards(seat, sortedCardsList.removeFirst());
          }
        }
      }
    } else {
      normalDealCards(table);
    }
  }

  /**
   * 玩家奖池控制发闲家牌 .
   * 
   * @param dealer . 庄家(牌和牌型已知)
   * @param seat 座位信息. 闲家(牌和牌型未知,需要计算出来)
   * @param sortedCardsList . 提前生成的排好序的牌
   * @return true: 被奖池控制了,false：没有被奖池控制
   */
  private boolean playerPrizePoolControlDealCards(SiRenNiuNiuSeat dealer, SiRenNiuNiuSeat seat,
      LinkedList<List<NiuNiuCard>> sortedCardsList) {
    SiRenNiuNiuTable table = dealer.getTable();
    // 控制输赢的玩家
    SiRenNiuNiuSeat controlSeat = dealer.isRobot() ? seat : dealer;
    PlayerPrizePool prizePool = playerPrizePoolMgr.prizePool(controlSeat.getPlayerId(), THIS_GAME);

    if (prizePool != null && prizePool.getPrize().get() != 0) {
      SiRenNiuNiuControl control = prizePool.control(new TypeReference<SiRenNiuNiuControl>() {});
      // 控制输赢的玩家的输赢标识
      boolean controlSeatWin = RandCodeUtil
          .random(control.getLoseRatio() + control.getWinRatio()) >= control.getLoseRatio();
      // 闲家输赢
      boolean seatWin = controlSeat == seat ? controlSeatWin : !controlSeatWin;

      List<NiuNiuCard> seatCards =
          seatWin ? sortedCardsList.removeLast() : sortedCardsList.removeFirst();
      dealPlayerCards(seat, seatCards);

      /*
       * 奖池为：60w,却赢了100w，则奖池达到要求变为0 . 奖池为：-60w,却输了100w，则奖池达到要求变为0
       */
      // 控制玩家赢得钱
      long winGold = 0;
      long prizeGold = prizePool.getPrize().get();
      if (controlSeat == seat) { // 控制的是闲家
        winGold = srnnMgr.convertGold(table.getRoom().getRoomDomain(),
            seatWin ? seat.getCardsType().multiple * seat.getBetedchips()
                : -dealer.getCardsType().multiple * seat.getBetedchips());
      } else { // 控制的是庄家
        winGold = srnnMgr.convertGold(table.getRoom().getRoomDomain(),
            seatWin ? -seat.getCardsType().multiple * seat.getBetedchips()
                : dealer.getCardsType().multiple * seat.getBetedchips());
      }

      if (prizeGold > 0) {
        prizeGold = prizeGold - winGold;
        prizeGold = prizeGold < 0 ? 0 : prizeGold;
      } else if (prizeGold < 0) {
        prizeGold = prizeGold - winGold;
        prizeGold = prizeGold > 0 ? 0 : prizeGold;
      }

      prizePool.getPrize().getAndSet(prizeGold);
      return true;
    }

    return false;
  }


  /**
   * 房间奖池控制发闲家牌 .
   * 
   * @param dealer . 庄家(牌和牌型已知)
   * @param seat 座位信息. 闲家(牌和牌型未知,需要在本方法中计算出来)
   * @param sortedCardsList . 提前生成的排好序的牌
   * @return true: 被奖池控制了,false：没有被奖池控制
   */
  private boolean roomPrizePoolControlDealCards(SiRenNiuNiuSeat dealer, SiRenNiuNiuSeat seat,
      LinkedList<List<NiuNiuCard>> sortedCardsList) {
    SiRenNiuNiuTable table = dealer.getTable();
    // 控制输赢的玩家
    SiRenNiuNiuSeat controlSeat = dealer.isRobot() ? seat : dealer;
    RoomPrizePool roomPrizePool = roomPrizePoolMgr.roomPrizePool(controlSeat.getPlayerId(),
        THIS_GAME, table.getRoom().getId());

    if (roomPrizePool != null) {
      // 房间奖池控制策略
      ControlStrategy strategy = roomPrizePool.abledControlStrategy();
      List<NiuNiuCard> seatCards = null;
      // 控制输赢的玩家的输赢标识
      boolean controlSeatWin;

      if (strategy != null) { // 有控制策略
        // 牛牛控制
        SiRenNiuNiuControl control = strategy.control(new TypeReference<SiRenNiuNiuControl>() {});
        controlSeatWin = RandCodeUtil
            .random(control.getLoseRatio() + control.getWinRatio()) >= control.getLoseRatio();
      } else { // 没有随机输赢
        controlSeatWin = RandCodeUtil.randomBoolean();
      }
      // 闲家输赢
      boolean seatWin = controlSeat == seat ? controlSeatWin : !controlSeatWin;

      seatCards = seatWin ? sortedCardsList.removeLast() : sortedCardsList.removeFirst();
      dealPlayerCards(seat, seatCards);

      long prizeGold = roomPrizePool.getPrize().get();
      // 控制玩家赢得钱
      long winGold = 0;

      if (controlSeat == seat) { // 控制的是闲家
        winGold = srnnMgr.convertGold(table.getRoom().getRoomDomain(),
            seatWin ? seat.getCardsType().multiple * seat.getBetedchips()
                : -dealer.getCardsType().multiple * seat.getBetedchips());
      } else { // 控制的是庄家
        winGold = srnnMgr.convertGold(table.getRoom().getRoomDomain(),
            seatWin ? -seat.getCardsType().multiple * seat.getBetedchips()
                : dealer.getCardsType().multiple * seat.getBetedchips());
      }

      prizeGold -= winGold;
      roomPrizePool.getPrize().getAndSet(prizeGold);
      return true;
    }

    return false;
  }

  // /**
  // * 控制玩家发牌,玩家和机器人玩才控制 .
  // * @param table .
  // */
  // public void controlDealCards(SiRenNiuNiuTable table) {
  // // 庄家
  // SiRenNiuNiuSeat dealer = table.dealer();
  // boolean hasPlayer = false;
  // boolean hasRobot = false;
  //
  // for (SiRenNiuNiuSeat s : table.seats) {
  // if (s.playerId > 0) {
  // if (s.robot) {
  // hasRobot = true;
  // } else {
  // hasPlayer = true;
  // }
  // }
  // }
  // // 玩家和机器人玩才控制，个人奖池控制优先房间奖池控制
  // if (hasPlayer && hasRobot) {
  // /*
  // * 控制发牌方法：先将7手玩家牌排序(从小到大),再根据庄家和奖池去计算玩家对应的手牌 .
  // */
  // // 需要从小到大排序(从小到大),一共7副手牌(庄家的牌在中间)
  // LinkedList<List<NiuNiuCard>> sortedCardsList = new LinkedList<>();
  //
  // /*
  // * 生成10副牌 .
  // */
  // int cardNum = CONTROL_HAND_CARDS_NUM;
  // int cardIndex = 0;
  // for (int i = 0; i < cardNum; i++) {
  // sortedCardsList
  // .add(table.cards.subList(cardIndex, cardIndex += SiRenNiuNiuMgr.PLAYER_CARDS));
  // }
  //
  // // 玩家的牌进行排序
  // sortedCardsList.sort((cards1, cards2) -> {
  // NiuNiuCardsType cards1Type = NiuNiuCardsTypeGetter.getCardsType(cards1, new ArrayList<>());
  // NiuNiuCardsType cards2Type = NiuNiuCardsTypeGetter.getCardsType(cards2, new ArrayList<>());
  // if (NiuNiuCardsTypeComparator.compare(cards1, cards1Type, cards2, cards2Type)) {
  // return 1;
  // } else {
  // return -1;
  // }
  // });
  //
  // Map<Integer, List<NiuNiuCard>> cardMaps = new HashMap<Integer, List<NiuNiuCard>>();
  // // 映射庄家应该发的牌
  // for (SiRenNiuNiuSeat seat : table.seats) {
  // if (seat.playerId > 0 && seat.playerId != dealer.playerId) {
  //
  // if ((dealer.robot && !seat.robot) || (!dealer.robot && seat.robot)) {
  // // 玩家个人奖池控制控制排序
  // if (playerPrizePoolControlOrder(dealer, seat, sortedCardsList, cardMaps)) {
  // continue;
  // }
  // // 玩家房间奖池控制排序
  // if (roomPrizePoolControlOrder(dealer, seat, sortedCardsList, cardMaps)) {
  // continue;
  // }
  // }
  //
  // // 没有奖池控制随机输赢取排序好的牌发
  // if (RandCodeUtil.randomBoolean()) {
  // seat.setPower(1);
  // cardMaps.put(seat.order, sortedCardsList.removeLast());
  // } else {
  // seat.setPower(-1);
  // cardMaps.put(seat.order, sortedCardsList.removeFirst());
  // }
  // }
  // }
  //
  // // 从剩余的列表中随机取出一个作为庄家的牌
  // List<NiuNiuCard> dealerCards =
  // sortedCardsList.remove(RandCodeUtil.random(sortedCardsList.size()));
  // // 给庄家发牌
  // dealDealerCards(dealer, dealerCards);
  // // 打乱位置
  // Collections.shuffle(table.seats);
  // // 升序位置排列
  // Collections.sort(table.seats, new Comparator<SiRenNiuNiuSeat>() {
  // @Override
  // public int compare(SiRenNiuNiuSeat o1, SiRenNiuNiuSeat o2) {
  // return o1.getPower() - o2.getPower();
  // }
  // });
  //
  // // 给闲家发牌
  // for (SiRenNiuNiuSeat seat : table.seats) {
  // if (seat.playerId > 0 && seat.playerId != dealer.playerId) {
  // List<NiuNiuCard> cardValue = cardMaps.get(seat.order);
  // // 发牌之前，计算奖池金币变化(无法准确计算：1.当跨多个策略时，控制输赢概率不一样, 2: 跨多个范围奖池时，可能有部分统计到其他范围)
  // if ((dealer.robot && !seat.robot) || (!dealer.robot && seat.robot)) {
  // // 玩家个人奖池奖金变化
  // if (!playerPrizePoolGoldChange(dealer, seat, cardValue)) {
  // // 玩家房间奖池奖金变化
  // roomPrizePoolGoldChange(dealer, seat, cardValue);
  // }
  // }
  // // 给闲家发牌
  // dealPlayerCards(seat, cardValue);
  // }
  // }
  // } else {
  // normalDealCards(table);
  // }
  // }
  //
  // /**
  // * 玩家奖池控制发闲家牌 .
  // * @param dealer . 庄家(牌和牌型已知)
  // * @param seat 座位信息. 闲家(牌和牌型未知,需要计算出来)
  // * @param sortedCardsList . 提前生成的排好序的牌
  // * @return true: 被奖池控制了,false：没有被奖池控制
  // */
  // private boolean playerPrizePoolControlOrder(SiRenNiuNiuSeat dealer, SiRenNiuNiuSeat seat,
  // LinkedList<List<NiuNiuCard>> sortedCardsList, Map<Integer, List<NiuNiuCard>> cardMaps) {
  // // 控制输赢的玩家
  // SiRenNiuNiuSeat controlSeat = dealer.robot ? seat : dealer;
  // PlayerPrizePool prizePool =
  // playerPrizePoolMgr.prizePool(controlSeat.playerId, THIS_GAME);
  //
  // if (prizePool != null && prizePool.getPrize().get() != 0) {
  // SiRenNiuNiuControl control = prizePool.control(new TypeReference<SiRenNiuNiuControl>() {});
  // // 控制输赢的玩家的输赢标识
  // boolean controlSeatWin = RandCodeUtil
  // .random(control.getLoseRatio() + control.getWinRatio()) >= control.getLoseRatio();
  // // 闲家输赢
  // boolean seatWin = controlSeat == seat ? controlSeatWin : !controlSeatWin;
  // if (seatWin) {
  // seat.setPower(1);
  // cardMaps.put(seat.order, sortedCardsList.removeLast());
  // } else {
  // cardMaps.put(seat.order, sortedCardsList.removeFirst());
  // seat.setPower(-1);
  // }
  // return true;
  // }
  // return false;
  // }
  //
  //
  // /**
  // * 房间奖池控制发闲家牌 .
  // * @param dealer . 庄家(牌和牌型已知)
  // * @param seat 座位信息. 闲家(牌和牌型未知,需要在本方法中计算出来)
  // * @param sortedCardsList . 提前生成的排好序的牌
  // * @return true: 被奖池控制了,false：没有被奖池控制
  // */
  // private boolean roomPrizePoolControlOrder(SiRenNiuNiuSeat dealer, SiRenNiuNiuSeat seat,
  // LinkedList<List<NiuNiuCard>> sortedCardsList, Map<Integer, List<NiuNiuCard>> cardMaps) {
  // SiRenNiuNiuTable table = dealer.table;
  // // 控制输赢的玩家
  // SiRenNiuNiuSeat controlSeat = dealer.robot ? seat : dealer;
  // RoomPrizePool roomPrizePool =
  // roomPrizePoolMgr.roomPrizePool(controlSeat.playerId, THIS_GAME, table.room.id);
  //
  // if (roomPrizePool != null) {
  // // 房间奖池控制策略
  // ControlStrategy strategy = roomPrizePool.abledControlStrategy();
  // // 控制输赢的玩家的输赢标识
  // boolean controlSeatWin;
  //
  // if (strategy != null) { // 有控制策略
  // // 牛牛控制
  // SiRenNiuNiuControl control = strategy.control(new TypeReference<SiRenNiuNiuControl>() {});
  // controlSeatWin = RandCodeUtil
  // .random(control.getLoseRatio() + control.getWinRatio()) >= control.getLoseRatio();
  //
  // // 闲家输赢
  // boolean seatWin = controlSeat == seat ? controlSeatWin : !controlSeatWin;
  //
  // if (seatWin) {
  // seat.setPower(1);
  // cardMaps.put(seat.order, sortedCardsList.removeLast());
  // } else {
  // cardMaps.put(seat.order, sortedCardsList.removeFirst());
  // seat.setPower(-1);
  // }
  // return true;
  // }
  // }
  // return false;
  // }
  //
  // /***
  // * 玩家奖池控制发闲家牌 .
  // * @param dealer . 庄家(牌和牌型已知)
  // * @param seat 座位信息. 闲家(牌和牌型未知,需要计算出来)
  // * @param sortedCardsList . 提前生成的排好序的牌
  // * @return true: 被奖池控制了,false：没有被奖池控制
  // */
  // private boolean playerPrizePoolGoldChange(SiRenNiuNiuSeat dealer, SiRenNiuNiuSeat seat,
  // List<NiuNiuCard> cardValue) {
  // SiRenNiuNiuTable table = dealer.table;
  // // 控制输赢的玩家
  // SiRenNiuNiuSeat controlSeat = dealer.robot ? seat : dealer;
  // PlayerPrizePool prizePool =
  // playerPrizePoolMgr.prizePool(controlSeat.playerId, THIS_GAME);
  //
  // if (prizePool != null && prizePool.getPrize().get() != 0) {
  // NiuNiuCardsType cardsType = NiuNiuCardsTypeGetter.getCardsType(cardValue, seat.bestCards);
  // // dealPlayerCards(seat, cardValue);
  //
  // /*
  // * 奖池为：60w,却赢了100w，则奖池达到要求变为0 . 奖池为：-60w,却输了100w，则奖池达到要求变为0
  // */
  // // 控制玩家赢得钱
  // long winGold = 0;
  // long prizeGold = prizePool.getPrize().get();
  // if (controlSeat == seat) { // 控制的是闲家
  // winGold = srnnMgr.convertGold(table.room.id,
  // seat.getPower() > 0 ? cardsType.multiple * seat.betedchips .
  // : -dealer.cardsType.multiple * seat.betedchips);
  // } else { // 控制的是庄家
  // winGold = srnnMgr.convertGold(table.room.id,
  // seat.getPower() > 0 ? cardsType.multiple * seat.betedchips .
  // : dealer.cardsType.multiple * seat.betedchips);
  // }
  //
  // if (prizeGold > 0) {
  // prizeGold = prizeGold - winGold;
  // prizeGold = prizeGold < 0 ? 0 : prizeGold;
  // } else if (prizeGold < 0) {
  // prizeGold = prizeGold - winGold;
  // prizeGold = prizeGold > 0 ? 0 : prizeGold;
  // }
  //
  // prizePool.getPrize().getAndSet(prizeGold);
  // return true;
  // }
  //
  // return false;
  // }
  //
  //
  // /**
  // * 房间奖池控制发闲家牌 .
  // * @param dealer . 庄家(牌和牌型已知)
  // * @param seat 座位信息. 闲家(牌和牌型未知,需要在本方法中计算出来)
  // * @param sortedCardsList . 提前生成的排好序的牌
  // * @return true: 被奖池控制了,false：没有被奖池控制
  // */
  // private boolean roomPrizePoolGoldChange(SiRenNiuNiuSeat dealer, SiRenNiuNiuSeat seat,
  // List<NiuNiuCard> cardValue) {
  // SiRenNiuNiuTable table = dealer.table;
  // // 控制输赢的玩家
  // SiRenNiuNiuSeat controlSeat = dealer.robot ? seat : dealer;
  // RoomPrizePool roomPrizePool =
  // roomPrizePoolMgr.roomPrizePool(controlSeat.playerId, THIS_GAME, table.room.id);
  //
  // if (roomPrizePool != null) {
  // // 房间奖池控制策略
  // ControlStrategy strategy = roomPrizePool.abledControlStrategy();
  // if (strategy != null) { // 有控制策略
  // NiuNiuCardsType cardsType = NiuNiuCardsTypeGetter.getCardsType(cardValue, seat.bestCards);
  //
  // long prizeGold = roomPrizePool.getPrize().get();
  // // 控制玩家赢得钱
  // long winGold = 0;
  //
  // if (controlSeat == seat) { // 控制的是闲家
  // winGold = srnnMgr.convertGold(table.room.id,
  // seat.getPower() > 0 ? cardsType.multiple * seat.betedchips .
  // : -dealer.cardsType.multiple * seat.betedchips);
  // } else { // 控制的是庄家
  // winGold = srnnMgr.convertGold(table.room.id,
  // seat.getPower() > 0 ? -cardsType.multiple * seat.betedchips .
  // : dealer.cardsType.multiple * seat.betedchips);
  // }
  //
  // prizeGold -= winGold;
  // roomPrizePool.getPrize().getAndSet(prizeGold);
  // return true;
  // }
  // }
  //
  // return false;
  // }

  /**
   * 正常发牌 .
   * 
   * @param table .
   */
  private void normalDealCards(SiRenNiuNiuTable table) {
    List<NiuNiuCard> tableCards = table.getCards();
    SiRenNiuNiuSeat dealer = table.dealer();
    int cardIndex = 0;

    // 先发庄家的牌,有了庄家的牌才能确定输赢计算筹码
    dealDealerCards(dealer,
        tableCards.subList(cardIndex, cardIndex += SiRenNiuNiuMgr.PLAYER_CARDS));

    /*
     * 再发闲家牌，由于先发的庄家牌，所以发闲家牌时已经能计算输赢筹码了 .
     */
    for (SiRenNiuNiuSeat seat : table.getSeats()) {
      if (seat.getPlayerId() > 0 && seat.getPlayerId() != dealer.getPlayerId()) {
        dealPlayerCards(seat,
            tableCards.subList(cardIndex, cardIndex += SiRenNiuNiuMgr.PLAYER_CARDS));
      }
    }
  }

  /**
   * 给庄家发牌 .
   * 
   * @param dealer .
   * @param cards .
   */
  private void dealDealerCards(SiRenNiuNiuSeat dealer, List<NiuNiuCard> cards) {
    NiuNiuCardsType cardsType = NiuNiuCardsTypeGetter.getCardsType(cards, dealer.getBestCards());
    dealer.getCards().clear();
    dealer.getCards().addAll(cards);
    dealer.setCardsType(cardsType);

    msgMgr.sendDealCardsMsg(dealer);
    LOG.info("[四人牛牛]发送庄家[{}][{}]牌[{}]", dealer.getPlayerId(), dealer.getPlayerName(),
        dealer.getBestCards());
  }

  /**
   * 给闲家发牌(由于庄家的牌先发，闲家发牌的同时输赢已经出来了，即billChips已经计算出来了) .
   * 
   * @param seat 座位信息.
   * @param cards .
   */
  private void dealPlayerCards(SiRenNiuNiuSeat seat, List<NiuNiuCard> cards) {
    NiuNiuCardsType seatCardsType = NiuNiuCardsTypeGetter.getCardsType(cards, seat.getBestCards());
    seat.getCards().clear();
    seat.getCards().addAll(cards);
    seat.setCardsType(seatCardsType);

    SiRenNiuNiuSeat dealer = seat.getTable().dealer();
    List<NiuNiuCard> dealerCards = dealer.getCards();
    NiuNiuCardsType dealerCardsType = dealer.getCardsType();

    boolean win = NiuNiuCardsTypeComparator.compare(seat.getCards(), seatCardsType, dealerCards,
        dealerCardsType);
    long betedchips = seat.getBetedchips();

    if (win) { // seat赢
      seat.setBillChips(seatCardsType.multiple * betedchips);
      dealer.setBillChips(dealer.getBillChips() - seatCardsType.multiple * betedchips);
    } else { // 庄家赢
      dealer.setBillChips(dealer.getBillChips() + dealerCardsType.multiple * betedchips);
      seat.setBillChips(-dealerCardsType.multiple * betedchips);
    }

    msgMgr.sendDealCardsMsg(seat);
    LOG.info("[四人牛牛]发送玩家[{}][{}]牌[{}]", seat.getPlayerId(), seat.getPlayerName(),
        seat.getBestCards());
  }
}
